package com.csu.cphone.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.csu.common.utils.RedisUtil;
import com.csu.cphone.dao.StoreMapper;
import com.csu.cphone.pojo.StorePojo;
import com.csu.cphone.service.StoreService;

/**
 * @项目名称：cphone
 * @类名称：StoreServiceImpl
 * @类描述：
 * @创建人：Tangc
 * @创建时间：2017年12月6日 上午11:52:02
 * @company:
 */
@Service
public class StoreServiceImpl implements StoreService
{
    @Autowired
    private StoreMapper storeMapper;

    private byte[] getKey()
    {
        return "store:position".getBytes();
    }

    private byte[] getMember(Integer id)
    {
        return String.valueOf(id).getBytes();
    }

    /**
     * @see com.csu.cphone.service.StoreService#add(java.lang.String,
     *      java.lang.Double, java.lang.Double, java.lang.String,
     *      java.lang.String, java.lang.Integer)
     */
    @Override
    public StorePojo add(String number, Double lng, Double lat, String name, String address, Integer producerId)
    {
        StorePojo store = new StorePojo();
        store.setAddress(address);
        store.setLat(lat);
        store.setLng(lng);
        store.setName(name);
        store.setNumber(number);
        store.setProducerId(producerId);
        storeMapper.insert(store);
        RedisUtil.getRedisDb().geoadd(getKey(), lng, lat, getMember(store.getId()));
        return store;
    }

    /**
     * @see com.csu.cphone.service.StoreService#update(com.csu.cphone.pojo.StorePojo)
     */
    @Override
    public Integer update(StorePojo store)
    {
        return storeMapper.update(store);
    }

    /**
     * @see com.csu.cphone.service.StoreService#queryById(java.lang.Integer)
     */
    @Override
    public StorePojo queryById(Integer storeId)
    {
        return storeMapper.selectById(storeId);
    }

    /**
     * @see com.csu.cphone.service.StoreService#count()
     */
    @Override
    public Integer count()
    {
        return storeMapper.count();
    }

    /**
     * @see com.csu.cphone.service.StoreService#queryList(java.lang.Integer,
     *      java.lang.Integer, java.lang.String)
     */
    @Override
    public List<StorePojo> queryList(Integer pageSize, Integer startIndex, String order)
    {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pageSize", pageSize);
        map.put("startIndex", startIndex);
        map.put("order", order);

        return storeMapper.queryList(map);
    }

    /**
     * @see com.csu.cphone.service.StoreService#deleteById(java.lang.Integer)
     */
    @Override
    public Integer deleteById(Integer storeId)
    {
        return storeMapper.deleteById(storeId);
    }

    /**
     * @see com.csu.cphone.service.StoreService#getByProducerId(java.lang.Integer)
     */
    @Override
    public StorePojo getByProducerId(Integer producerId)
    {
        return storeMapper.selectByProducerId(producerId);
    }

    /**
     * @see com.csu.cphone.service.StoreService#updateByProducerId(com.csu.cphone.pojo.StorePojo)
     */
    @Override
    public Integer updateByProducerId(StorePojo store)
    {
        return storeMapper.updateByProducerId(store);
    }

}
